const { series, watch } = require("gulp");
const electron = require("electron-connect").server.create();

function serve(done) {
  electron.start(() => {
    watch("main.js", restart);
    watch(["./render/**/*.js", "./render/**/*.html"], reload);
  });
  done();
} 

function restart(done) {
  electron.restart("--enable-logging", function(state) {
    if (state === "restarted" || state === "restarting") {
      done();
    } else {
      done(
        "Unexpected state while restarting electron-connect server. State " +
          state
      );
    }
  });
}

function reload(done) {
  electron.reload();
  done();
}

exports.default = series(serve);
